package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.ChargeType;
import com.example.result.Result;
import com.example.service.ChargeTypeService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 费用类型控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class ChargeTypeController {

    @Autowired
    ChargeTypeService chargeTypeService;

    //===============================费用类型管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加费用类型信息
     */
    @RequestMapping(value = "/chargeTypes", method = RequestMethod.POST)
    public Result addChargeType(@RequestBody String json) {
        //将JSON数据转换为ChargeType对象实体
        JSONObject parseObject = JSON.parseObject(json);
        ChargeType chargeType = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), ChargeType.class);

        return chargeTypeService.addChargeType(chargeType);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新费用类型信息
     */
    @RequestMapping(value = "/chargeTypes", method = RequestMethod.PUT)
    public Result updateChargeType(@RequestBody String json) {
        //将JSON数据转换为ChargeType对象实体
        JSONObject parseObject = JSON.parseObject(json);
        ChargeType chargeType = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), ChargeType.class);

        return chargeTypeService.updateChargeType(chargeType);

    }

    /**
     * @param chargeTypeId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据编号删除费用类型信息
     */
    @RequestMapping(value = "/chargeTypes/{chargeTypeId}", method = RequestMethod.DELETE)
    public Result deleteChargeTypeById(@PathVariable("chargeTypeId") String chargeTypeId) {
        return chargeTypeService.deleteChargeTypeById(chargeTypeId);

    }

    /**
     * @return 以JSON格式返回所有费用信息
     * @describe 查询所有费用类型信息
     */
    @RequestMapping(value = "/chargeTypes", method = RequestMethod.GET)
    public Result queryAllChargeType() {
        return chargeTypeService.queryAllChargeType();

    }

    /**
     * @param pid
     * @return 以JSON格式返回该费用类型名称信息
     * @describe 根据父ID查询费用类型名称信息
     */
    @RequestMapping(value = "/chargeTypes/{pid}", method = RequestMethod.GET)
    public Result queryChargeTypeByPid(@PathVariable("pid") String pid) {
        return chargeTypeService.queryChargeTypeByPid(pid);
    }

    /**
     * @param chargeTypeId
     * @return 以JSON格式返回该费用类型信息
     * @describe 根据编号查询费用类型信息
     */
    @RequestMapping(value = "/chargeTypes/{chargeTypeId}", method = RequestMethod.GET)
    public Result queryChargeTypeById(@PathVariable("chargeTypeId") String chargeTypeId) {
        return chargeTypeService.queryChargeTypeById(chargeTypeId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加费用类型信息
     */
    @RequestMapping(value = "/chargeTypes/excels", method = RequestMethod.POST)
    public Result addChargeTypeByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<ChargeType> list = JSON.parseArray(parseObject.getString("json"), ChargeType.class);

        System.out.println(list);

        return chargeTypeService.addChargeTypeByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据费用类型编号批量删除费用类型信息
     */
    @RequestMapping(value = "/chargeTypes/dels", method = RequestMethod.POST)
    public Result deleteChargeTypeByIds(@RequestBody Map<String, String> Ids) {

        return chargeTypeService.deleteChargeTypeByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 费用类型信息模糊查询
     */
    @RequestMapping(value = "/chargeTypes/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return chargeTypeService.serachByInfo(info);

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 费用类型树形结构信息查询
     */
    @RequestMapping(value = "/chargeTypes/trees", method = RequestMethod.GET)
    public Result queryAllOfTreeForm() {
        return chargeTypeService.queryAllOfTreeForm();
    }
}
